package swing;

import javax.swing.*;
import java.awt.*;

/**
 * 布局演示
 * @author fidel
 */
public class LayoutDemo {
    private static int i;
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            JFrame form = new JFrame();
            form.setVisible(true);
            form.setTitle("布局测试");
            form.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            form.getContentPane().applyComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
            // 把内容面板分成4个部分
            form.getContentPane().setLayout(new GridLayout(2, 2));
            JPanel cardPanel = new JPanel();
            JPanel borderPanel = new JPanel();
            JPanel gridPanel = new JPanel();
            JPanel flowPanel = new JPanel();
            form.getContentPane().add(cardPanel);
            form.getContentPane().add(borderPanel);
            form.getContentPane().add(gridPanel);
            // 每个部分各放置一个面板
            form.getContentPane().add(flowPanel);
            CardLayout card = new CardLayout();
            cardPanel.setLayout(card);
            JButton button0 = new JButton("0");
            JButton button1 = new JButton("1");
            JButton button2 = new JButton("2");
            cardPanel.add(button0);
            cardPanel.add(button1);
            cardPanel.add(button2);
            card.addLayoutComponent(button0, "0");
            card.addLayoutComponent(button1, "1");
            card.addLayoutComponent(button2, "2");
            BorderLayout border = new BorderLayout();
            borderPanel.setLayout(border);
            JButton button3 = new JButton("EAST");
            JButton button4 = new JButton("WEST");
            JButton button5 = new JButton("NORTH");
            JButton button6 = new JButton("SOUTH");
            JButton button7 = new JButton("CENTER");
            borderPanel.add(button3);
            borderPanel.add(button4);
            borderPanel.add(button5);
            borderPanel.add(button6);
            borderPanel.add(button7);
            border.addLayoutComponent(button3, BorderLayout.EAST);
            border.addLayoutComponent(button4, BorderLayout.WEST);
            border.addLayoutComponent(button5, BorderLayout.NORTH);
            border.addLayoutComponent(button6, BorderLayout.SOUTH);
            border.addLayoutComponent(button7, BorderLayout.CENTER);
            JButton button8 = new JButton("8");
            JButton button9 = new JButton("9");
            JButton button10 = new JButton("10");
            JButton button11 = new JButton("11（更改排序）");
            gridPanel.add(button8);
            gridPanel.add(button9);
            gridPanel.add(button10);
            gridPanel.add(button11);
            GridLayout grid = new GridLayout();
            grid.setColumns(2);
            grid.setRows(2);
            gridPanel.setLayout(grid);
            button11.addActionListener(e -> {
                i = i + 1;
                if (i % 2 != 0) {
                    gridPanel.removeAll();
                    gridPanel.add(button10);
                    gridPanel.add(button11);
                    gridPanel.add(button8);
                    gridPanel.add(button9);
                    gridPanel.updateUI();
                } else {
                    gridPanel.removeAll();
                    gridPanel.add(button8);
                    gridPanel.add(button9);
                    gridPanel.add(button10);
                    gridPanel.add(button11);
                    gridPanel.updateUI();
                }
            });
            flowPanel.add(new JButton("12"));
            flowPanel.add(new JButton("13"));
            flowPanel.add(new JButton("14"));
            form.pack();
            form.setLocationRelativeTo(null);
        });
    }
}
